#include "general.h"
#if (SENSOR_TYPE == Sensor_GC0316)
#include "pv_api.h"
#include "sensor_cdsp.h"
#include "sp1k_aaa_api.h"
#include "hal.h"
UINT8 sensorDriverGainClass = 0xff;
UINT8	RawR[256],RawG[256],RawB[256];
UINT32 snapexp;
UINT8 snapgain;
//UINT8 ttttt=30;
#if 0
#define MAX_FRAME_RATE_30_IDX 105	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 5	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 105	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 5	// New limited for OV3630 - Jim@0424

#define MAX_FRAME_RATE_25_IDX 105
#define MIN_FRAME_RATE_25_IDX 5
#define MAX_FRAME_RATE_12P5_IDX 105
#define MIN_FRAME_RATE_12P5_IDX 5

#define MAX_GAIN_IDX (191)
#define MIN_GAIN_IDX  (0)

#define K_GAIN_Large       180
#define K_GAIN_Middle	100
#define K_GAIN_Small	60

code AEstr LVtbl50[108] = {
		{0,	 250,	 191,	 0	},
		{1,	 250,	 181,	 0	},
		{2,	 250,	 165,	 0	},
		{3,	 250,	 149,	 0	},
		{4,	 250,	 135,	 0	},
		{5,	 250,	 122,	 0	},
		{6,	 250,	 109,	 0	},
		{7,	 332,	 151,	 0	},
		{8,	 250,	 87,	 0	},
		{9,	 332,	 123,	 0	},
		{10,	 334,	 111,	 0	},
		{11,	 502,	 182,	 0	},
		{12,	 332,	 88,	 0	},
		{13,	 499,	 149,	 0	},
		{14,	 499,	 135,	 0	},
		{15,	 250,	 29,	 0	},
		{16,	 499,	 109,	 0	},
		{17,	 250,	 17,	 0	},
		{18,	 502,	 87,	 0	},
		{19,	 502,	 77,	 0	},
		{20,	 499,	 67,	 0	},
		{21,	 502,	 59,	 0	},
		{22,	 332,	 12,	 0	},
		{23,	 1004,	 150,	 0	},
		{24,	 994,	 134,	 0	},
		{25,	 1014,	 124,	 0	},
		{26,	 502,	 23,	 0	},
		{27,	 1014,	 100,	 0	},
		{28,	 1014,	 89,	 0	},
		{29,	 994,	 76,	 0	},
		{30,	 1014,	 69,	 0	},
		{31,	 1004,	 59,	 0	},
		{32,	 1014,	 52,	 0	},
		{33,	 1004,	 43,	 0	},
		{34,	 994,	 35,	 0	},
		{35,	 1014,	 30,	 0	},
		{36,	 1004,	 23,	 0	},
		{37,	 1014,	 18,	 0	},
		{38,	 994,	 11,	 0	},
		{39,	 994,	 6,	 0	},
		{40,	 1004,	 2,	 0	},
		{41,	 3169,	 130,	 0	},
		{42,	 3169,	 117,	 0	},
		{43,	 2157,	 51,	 0	},
		{44,	 2473,	 59,	 0	},
		{45,	 3900,	 117,	 0	},
		{46,	 5337,	 167,	 0	},
		{47,	 1584,	 0,	 0	},
		{48,	 2069,	 14,	 0	},
		{49,	 4408,	 91,	 0	},
		{50,	 1950,	 0,	 0	},
		{51,	 6337,	 130,	 0	},
		{52,	 6337,	 117,	 0	},
		{53,	 7242,	 129,	 0	},
		{54,	 3900,	 33,	 0	},
		{55,	 7799,	 117,	 0	},
		{56,	 5633,	 58,	 0	},
		{57,	 3169,	 0,	 0	},
		{58,	 4828,	 27,	 0	},
		{59,	 11266,	 134,	 0	},
		{60,	 3900,	 0,	 0	},
		{61,	 12674,	 130,	 0	},
		{62,	 12674,	 117,	 0	},
		{63,	 14485,	 129,	 0	},
		{64,	 7799,	 33,	 0	},
		{65,	 16899,	 132,	 0	},
		{66,	 11266,	 58,	 0	},
		{67,	 6337,	 0,	 0	},
		{68,	 20278,	 127,	 0	},
		{69,	 11266,	 35,	 0	},
		{70,	 7799,	 0,	 0	},
		{71,	 25348,	 130,	 0	},
		{72,	 25348,	 117,	 0	},
		{73,	 25348,	 105,	 0	},
		{74,	 33797,	 146,	 0	},
		{75,	 33797,	 132,	 0	},
		{76,	 33797,	 119,	 0	},
		{77,	 12674,	 0,	 0	},
		{78,	 50695,	 175,	 0	},
		{79,	 50695,	 159,	 0	},
		{80,	 25348,	 40,	 0	},
		{81,	 50695,	 130,	 0	},
		{82,	 50695,	 117,	 0	},
		{83,	 50695,	 105,	 0	},
		{84,	 33797,	 41,	 0	},
		{85,	 50695,	 83,	 0	},
		{86,	 50695,	 73,	 0	},
		{87,	 25348,	 0,	 0	},
		{88,	 101384,	 175,	 0	},
		{89,	 101384,	 159,	 0	},
		{90,	 50695,	 40,	 0	},
		{91,	 50695,	 33,	 0	},
		{92,	 101384,	 117,	 0	},
		{93,	 101384,	 105,	 0	},
		{94,	 50695,	 15,	 0	},
		{95,	 101384,	 83,	 0	},
		{96,	 101384,	 73,	 0	},
		{97,	 50695,	 0,	 0	},
		{98,	 101384,	 55,	 0	},
		{99,	 101384,	 47,	 0	},
		{100,	 101384,	 40,	 0	},
		{101,	 101384,	 33,	 0	},
		{102,	 101384,	 27,	 0	},
		{103,	 101384,	 20,	 0	},
		{104,	 101384,	 15,	 0	},
		{105,	 101384,	 10,	 0	},
		{106,	 101384,	 5,	 0	},
		{107,	 101384,	 0,	 0	},
	};



code AEstr LVtbl60[108] = {
		{0,	 240,	 188,	 0	},
		{1,	 240,	 171,	 0	},
		{2,	 240,	 155,	 0	},
		{3,	 240,	 141,	 0	},
		{4,	 240,	 127,	 0	},
		{5,	 240,	 114,	 0	},
		{6,	 240,	 102,	 0	},
		{7,	 240,	 91,	 0	},
		{8,	 300,	 117,	 0	},
		{9,	 240,	 71,	 0	},
		{10,	 240,	 62,	 0	},
		{11,	 300,	 83,	 0	},
		{12,	 300,	 73,	 0	},
		{13,	 401,	 107,	 0	},
		{14,	 399,	 95,	 0	},
		{15,	 240,	 25,	 0	},
		{16,	 240,	 19,	 0	},
		{17,	 399,	 65,	 0	},
		{18,	 600,	 117,	 0	},
		{19,	 600,	 105,	 0	},
		{20,	 401,	 41,	 0	},
		{21,	 600,	 83,	 0	},
		{22,	 600,	 73,	 0	},
		{23,	 300,	 0,	 0	},
		{24,	 1222,	 179,	 0	},
		{25,	 1207,	 160,	 0	},
		{26,	 1207,	 145,	 0	},
		{27,	 1207,	 131,	 0	},
		{28,	 1207,	 118,	 0	},
		{29,	 1222,	 108,	 0	},
		{30,	 1222,	 96,	 0	},
		{31,	 1193,	 82,	 0	},
		{32,	 1207,	 74,	 0	},
		{33,	 600,	 0,	 0	},
		{34,	 1207,	 56,	 0	},
		{35,	 1207,	 48,	 0	},
		{36,	 1222,	 42,	 0	},
		{37,	 1222,	 35,	 0	},
		{38,	 1207,	 27,	 0	},
		{39,	 1222,	 22,	 0	},
		{40,	 1222,	 16,	 0	},
		{41,	 1193,	 9,	 0	},
		{42,	 1207,	 5,	 0	},
		{43,	 1222,	 1,	 0	},
		{44,	 2473,	 59,	 0	},
		{45,	 3900,	 117,	 0	},
		{46,	 5337,	 167,	 0	},
		{47,	 1584,	 0,	 0	},
		{48,	 2069,	 14,	 0	},
		{49,	 4408,	 91,	 0	},
		{50,	 1950,	 0,	 0	},
		{51,	 6337,	 130,	 0	},
		{52,	 6337,	 117,	 0	},
		{53,	 7242,	 129,	 0	},
		{54,	 3900,	 33,	 0	},
		{55,	 7799,	 117,	 0	},
		{56,	 5633,	 58,	 0	},
		{57,	 3169,	 0,	 0	},
		{58,	 4828,	 27,	 0	},
		{59,	 11266,	 134,	 0	},
		{60,	 3900,	 0,	 0	},
		{61,	 12674,	 130,	 0	},
		{62,	 12674,	 117,	 0	},
		{63,	 14485,	 129,	 0	},
		{64,	 7799,	 33,	 0	},
		{65,	 16899,	 132,	 0	},
		{66,	 11266,	 58,	 0	},
		{67,	 6337,	 0,	 0	},
		{68,	 20278,	 127,	 0	},
		{69,	 11266,	 35,	 0	},
		{70,	 7799,	 0,	 0	},
		{71,	 25348,	 130,	 0	},
		{72,	 25348,	 117,	 0	},
		{73,	 25348,	 105,	 0	},
		{74,	 33797,	 146,	 0	},
		{75,	 33797,	 132,	 0	},
		{76,	 33797,	 119,	 0	},
		{77,	 12674,	 0,	 0	},
		{78,	 50695,	 175,	 0	},
		{79,	 50695,	 159,	 0	},
		{80,	 25348,	 40,	 0	},
		{81,	 50695,	 130,	 0	},
		{82,	 50695,	 117,	 0	},
		{83,	 50695,	 105,	 0	},
		{84,	 33797,	 41,	 0	},
		{85,	 50695,	 83,	 0	},
		{86,	 50695,	 73,	 0	},
		{87,	 25348,	 0,	 0	},
		{88,	 101384,	 175,	 0	},
		{89,	 101384,	 159,	 0	},
		{90,	 50695,	 40,	 0	},
		{91,	 50695,	 33,	 0	},
		{92,	 101384,	 117,	 0	},
		{93,	 101384,	 105,	 0	},
		{94,	 50695,	 15,	 0	},
		{95,	 101384,	 83,	 0	},
		{96,	 101384,	 73,	 0	},
		{97,	 50695,	 0,	 0	},
		{98,	 101384,	 55,	 0	},
		{99,	 101384,	 47,	 0	},
		{100,	 101384,	 40,	 0	},
		{101,	 101384,	 33,	 0	},
		{102,	 101384,	 27,	 0	},
		{103,	 101384,	 20,	 0	},
		{104,	 101384,	 15,	 0	},
		{105,	 101384,	 10,	 0	},
		{106,	 101384,	 5,	 0	},
		{107,	 101384,	 0,	 0	},
	};



code UINT8 gaintbl[192] = {
	64, 	65, 	66, 	67, 	68, 	69, 	70, 	71, 	72, 	73,
	74, 	75, 	76, 	77, 	78, 	79, 	80, 	81, 	82, 	83,
	84, 	85, 	86, 	87, 	88, 	89, 	90, 	91, 	92, 	93,
	94, 	95, 	96, 	97, 	98, 	99, 	100, 	101, 	102, 	103,
	104, 	105, 	106, 	107, 	108, 	109, 	110, 	111, 	112, 	113,
	114, 	115, 	116, 	117, 	118, 	119, 	120, 	121, 	122, 	123,
	124, 	125, 	126, 	127, 	128, 	129, 	130, 	131, 	132, 	133,
	134, 	135, 	136, 	137, 	138, 	139, 	140, 	141, 	142, 	143,
	144, 	145, 	146, 	147, 	148, 	149, 	150, 	151, 	152, 	153,
	154, 	155, 	156, 	157, 	158, 	159, 	160, 	161, 	162, 	163,
	164, 	165, 	166, 	167, 	168, 	169, 	170, 	171, 	172, 	173,
	174, 	175, 	176, 	177, 	178, 	179, 	180, 	181, 	182, 	183,
	184, 	185, 	186, 	187, 	188, 	189, 	190, 	191, 	192, 	193,
	194, 	195, 	196, 	197, 	198, 	199, 	200, 	201, 	202, 	203,
	204, 	205, 	206, 	207, 	208, 	209, 	210, 	211, 	212, 	213,
	214, 	215, 	216, 	217, 	218, 	219, 	220, 	221, 	222, 	223,
	224, 	225, 	226, 	227, 	228, 	229, 	230, 	231, 	232, 	233,
	234, 	235, 	236, 	237, 	238, 	239, 	240, 	241, 	242, 	243,
	244, 	245, 	246, 	247, 	248, 	249, 	250, 	251, 	252, 	253,
	254, 	255, 	};

#else

#define MAX_FRAME_RATE_30_IDX 105	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_30_IDX 1	// New limited for OV3630 - Jim@0424
#define MAX_FRAME_RATE_15_IDX 105	// New limited for OV3630 - Jim@0424
#define MIN_FRAME_RATE_15_IDX 1	// New limited for OV3630 - Jim@0424

#define MAX_FRAME_RATE_25_IDX 105
#define MIN_FRAME_RATE_25_IDX 1
#define MAX_FRAME_RATE_12P5_IDX 105
#define MIN_FRAME_RATE_12P5_IDX 1

#define MAX_GAIN_IDX (185)
#define MIN_GAIN_IDX  (0)

#define K_GAIN_Large       180
#define K_GAIN_Middle	100
#define K_GAIN_Small	60

code AEstr LVtbl50[109] = {
		{0,	 333,	 185,	 0	},
		{1,	 334,	 170,	 0	},
		{2,	 334,	 154,	 0	},
		{3,	 333,	 139,	 0	},
		{4,	 334,	 126,	 0	},
		{5,	 334,	 113,	 0	},
		{6,	 334,	 101,	 0	},
		{7,	 334,	 90,	 0	},
		{8,	 334,	 80,	 0	},
		{9,	 334,	 70,	 0	},
		{10,	 499,	 123,	 0	},
		{11,	 334,	 53,	 0	},
		{12,	 502,	 100,	 0	},
		{13,	 502,	 89,	 0	},
		{14,	 334,	 31,	 0	},
		{15,	 502,	 69,	 0	},
		{16,	 994,	 182,	 0	},
		{17,	 1001,	 167,	 0	},
		{18,	 1007,	 153,	 0	},
		{19,	 1001,	 137,	 0	},
		{20,	 1007,	 125,	 0	},
		{21,	 994,	 110,	 0	},
		{22,	 502,	 18,	 0	},
		{23,	 499,	 12,	 0	},
		{24,	 1014,	 80,	 0	},
		{25,	 994,	 68,	 0	},
		{26,	 994,	 59,	 0	},
		{27,	 1014,	 53,	 0	},
		{28,	 994,	 43,	 0	},
		{29,	 994,	 36,	 0	},
		{30,	 1014,	 31,	 0	},
		{31,	 994,	 23,	 0	},
		{32,	 994,	 17,	 0	},
		{33,	 1001,	 12,	 0	},
		{34,	 1014,	 8,	 0	},
		{35,	 994,	 2,	 0	},
		{36,	 1568,	 33,	 0	},
		{37,	 4002,	 167,	 0	},
		{38,	 1188,	 0,	 0	},
		{39,	 1552,	 14,	 0	},
		{40,	 4906,	 166,	 0	},
		{41,	 2765,	 57,	 0	},
		{42,	 4753,	 130,	 0	},
		{43,	 4753,	 117,	 0	},
		{44,	 3236,	 51,	 0	},
		{45,	 3710,	 59,	 0	},
		{46,	 5850,	 117,	 0	},
		{47,	 8005,	 167,	 0	},
		{48,	 2376,	 0,	 0	},
		{49,	 3104,	 14,	 0	},
		{50,	 6613,	 91,	 0	},
		{51,	 2925,	 0,	 0	},
		{52,	 9506,	 130,	 0	},
		{53,	 9506,	 117,	 0	},
		{54,	 10864,	 129,	 0	},
		{55,	 5850,	 33,	 0	},
		{56,	 11699,	 117,	 0	},
		{57,	 8449,	 58,	 0	},
		{58,	 4753,	 0,	 0	},
		{59,	 7242,	 27,	 0	},
		{60,	 16899,	 134,	 0	},
		{61,	 5850,	 0,	 0	},
		{62,	 19011,	 130,	 0	},
		{63,	 19011,	 117,	 0	},
		{64,	 21727,	 129,	 0	},
		{65,	 11699,	 33,	 0	},
		{66,	 25348,	 132,	 0	},
		{67,	 16899,	 58,	 0	},
		{68,	 9506,	 0,	 0	},
		{69,	 30418,	 127,	 0	},
		{70,	 16899,	 35,	 0	},
		{71,	 11699,	 0,	 0	},
		{72,	 38022,	 130,	 0	},
		{73,	 38022,	 117,	 0	},
		{74,	 38022,	 105,	 0	},
		{75,	 50695,	 146,	 0	},
		{76,	 50695,	 132,	 0	},
		{77,	 50695,	 119,	 0	},
		{78,	 19011,	 0,	 0	},
		{79,	 76042,	 175,	 0	},
		{80,	 76042,	 159,	 0	},
		{81,	 38022,	 40,	 0	},
		{82,	 76042,	 130,	 0	},
		{83,	 76042,	 117,	 0	},
		{84,	 76042,	 105,	 0	},
		{85,	 50695,	 41,	 0	},
		{86,	 76042,	 83,	 0	},
		{87,	 76042,	 73,	 0	},
		{88,	 38022,	 0,	 0	},
		{89,	 152076,	 175,	 0	},
		{90,	 152076,	 159,	 0	},
		{91,	 76042,	 40,	 0	},
		{92,	 76042,	 33,	 0	},
		{93,	 152076,	 117,	 0	},
		{94,	 152076,	 105,	 0	},
		{95,	 76042,	 15,	 0	},
		{96,	 152076,	 83,	 0	},
		{97,	 152076,	 73,	 0	},
		{98,	 76042,	 0,	 0	},
		{99,	 152076,	 55,	 0	},
		{100,	 152076,	 47,	 0	},
		{101,	 152076,	 40,	 0	},
		{102,	 152076,	 33,	 0	},
		{103,	 152076,	 27,	 0	},
		{104,	 152076,	 20,	 0	},
		{105,	 152076,	 15,	 0	},
		{106,	 152076,	 10,	 0	},
		{107,	 152076,	 5,	 0	},
		{108,	 152076,	 0,	 0	},
	};



code AEstr LVtbl60[106] = {
		{0,	 400,	 180,	 0	},
		{1,	 399,	 163,	 0	},
		{2,	 401,	 149,	 0	},
		{3,	 400,	 134,	 0	},
		{4,	 400,	 121,	 0	},
		{5,	 399,	 108,	 0	},
		{6,	 400,	 97,	 0	},
		{7,	 400,	 86,	 0	},
		{8,	 400,	 76,	 0	},
		{9,	 401,	 67,	 0	},
		{10,	 400,	 58,	 0	},
		{11,	 601,	 107,	 0	},
		{12,	 399,	 42,	 0	},
		{13,	 400,	 35,	 0	},
		{14,	 601,	 75,	 0	},
		{15,	 599,	 65,	 0	},
		{16,	 1188,	 175,	 0	},
		{17,	 1188,	 159,	 0	},
		{18,	 1188,	 144,	 0	},
		{19,	 1188,	 130,	 0	},
		{20,	 1188,	 117,	 0	},
		{21,	 1217,	 109,	 0	},
		{22,	 1207,	 96,	 0	},
		{23,	 1188,	 83,	 0	},
		{24,	 1188,	 73,	 0	},
		{25,	 1188,	 64,	 0	},
		{26,	 1217,	 58,	 0	},
		{27,	 1217,	 50,	 0	},
		{28,	 1188,	 40,	 0	},
		{29,	 1188,	 33,	 0	},
		{30,	 1207,	 28,	 0	},
		{31,	 1198,	 21,	 0	},
		{32,	 1207,	 16,	 0	},
		{33,	 1198,	 10,	 0	},
		{34,	 1198,	 5,	 0	},
		{35,	 1188,	 0,	 0	},
		{36,	 1552,	 14,	 0	},
		{37,	 4906,	 166,	 0	},
		{38,	 2765,	 57,	 0	},
		{39,	 4753,	 130,	 0	},
		{40,	 4753,	 117,	 0	},
		{41,	 3236,	 51,	 0	},
		{42,	 3710,	 59,	 0	},
		{43,	 5850,	 117,	 0	},
		{44,	 8005,	 167,	 0	},
		{45,	 2376,	 0,	 0	},
		{46,	 3104,	 14,	 0	},
		{47,	 6613,	 91,	 0	},
		{48,	 2925,	 0,	 0	},
		{49,	 9506,	 130,	 0	},
		{50,	 9506,	 117,	 0	},
		{51,	 10864,	 129,	 0	},
		{52,	 5850,	 33,	 0	},
		{53,	 11699,	 117,	 0	},
		{54,	 8449,	 58,	 0	},
		{55,	 4753,	 0,	 0	},
		{56,	 7242,	 27,	 0	},
		{57,	 16899,	 134,	 0	},
		{58,	 5850,	 0,	 0	},
		{59,	 19011,	 130,	 0	},
		{60,	 19011,	 117,	 0	},
		{61,	 21727,	 129,	 0	},
		{62,	 11699,	 33,	 0	},
		{63,	 25348,	 132,	 0	},
		{64,	 16899,	 58,	 0	},
		{65,	 9506,	 0,	 0	},
		{66,	 30418,	 127,	 0	},
		{67,	 16899,	 35,	 0	},
		{68,	 11699,	 0,	 0	},
		{69,	 38022,	 130,	 0	},
		{70,	 38022,	 117,	 0	},
		{71,	 38022,	 105,	 0	},
		{72,	 50695,	 146,	 0	},
		{73,	 50695,	 132,	 0	},
		{74,	 50695,	 119,	 0	},
		{75,	 19011,	 0,	 0	},
		{76,	 76042,	 175,	 0	},
		{77,	 76042,	 159,	 0	},
		{78,	 38022,	 40,	 0	},
		{79,	 76042,	 130,	 0	},
		{80,	 76042,	 117,	 0	},
		{81,	 76042,	 105,	 0	},
		{82,	 50695,	 41,	 0	},
		{83,	 76042,	 83,	 0	},
		{84,	 76042,	 73,	 0	},
		{85,	 38022,	 0,	 0	},
		{86,	 152076,	 175,	 0	},
		{87,	 152076,	 159,	 0	},
		{88,	 76042,	 40,	 0	},
		{89,	 76042,	 33,	 0	},
		{90,	 152076,	 117,	 0	},
		{91,	 152076,	 105,	 0	},
		{92,	 76042,	 15,	 0	},
		{93,	 152076,	 83,	 0	},
		{94,	 152076,	 73,	 0	},
		{95,	 76042,	 0,	 0	},
		{96,	 152076,	 55,	 0	},
		{97,	 152076,	 47,	 0	},
		{98,	 152076,	 40,	 0	},
		{99,	 152076,	 33,	 0	},
		{100,	 152076,	 27,	 0	},
		{101,	 152076,	 20,	 0	},
		{102,	 152076,	 15,	 0	},
		{103,	 152076,	 10,	 0	},
		{104,	 152076,	 5,	 0	},
		{105,	 152076,	 0,	 0	},
	};

code UINT8 gaintbl[186] = {
	64, 	65, 	66, 	67, 	68, 	69, 	70, 	71, 	72, 	73,
	74, 	75, 	76, 	77, 	78, 	79, 	80, 	81, 	82, 	83,
	84, 	85, 	86, 	87, 	88, 	89, 	90, 	91, 	92, 	93,
	94, 	95, 	96, 	97, 	98, 	99, 	100, 	101, 	102, 	103,
	104, 	105, 	106, 	107, 	108, 	109, 	110, 	111, 	112, 	113,
	114, 	115, 	116, 	117, 	118, 	119, 	120, 	121, 	122, 	123,
	124, 	125, 	126, 	127, 	128, 	129, 	130, 	131, 	132, 	133,
	134, 	135, 	136, 	137, 	138, 	139, 	140, 	141, 	142, 	143,
	144, 	145, 	146, 	147, 	148, 	149, 	150, 	151, 	152, 	153,
	154, 	155, 	156, 	157, 	158, 	159, 	160, 	161, 	162, 	163,
	164, 	165, 	166, 	167, 	168, 	169, 	170, 	171, 	172, 	173,
	174, 	175, 	176, 	177, 	178, 	179, 	180, 	181, 	182, 	183,
	184, 	185, 	186, 	187, 	188, 	189, 	190, 	191, 	192, 	193,
	194, 	195, 	196, 	197, 	198, 	199, 	200, 	201, 	202, 	203,
	204, 	205, 	206, 	207, 	208, 	209, 	210, 	211, 	212, 	213,
	214, 	215, 	216, 	217, 	218, 	219, 	220, 	221, 	222, 	223,
	224, 	225, 	226, 	227, 	228, 	229, 	230, 	231, 	232, 	233,
	234, 	235, 	236, 	237, 	238, 	239, 	240, 	241, 	242, 	243,
	244, 	245, 	246, 	247, 	248, 	249, 	};

#endif

code UINT16 cmt[3][9] = {
	{0x006C,0x0391,0x0043,0x000B,0x03F2,0x0043,0x0014,0x0377,0x00B5},
	{0x005B,0x03A4,0x0041,0x000F,0x03EE,0x0043,0x0015,0x038B,0x009F},
	{0x0046,0x03B9,0x0041,0x0015,0x03E7,0x0044,0x0019,0x03A6,0x0081}};


#define	DAY_H_INDEX			26
#define	DAY_L_INDEX			16
#define	CWF_H_INDEX			16
#define	CWF_L_INDEX			8
#define	TUNG_H_INDEX			8
#define	TUNG_L_INDEX			0
#define	CLOUDY_H_INDEX			29
#define	CLOUDY_L_INDEX			20

#define	AWBCTT_TBL_MAX			30

UINT8 gDAY_H_INDEX;
UINT8 gDAY_L_INDEX;
UINT8 gCWF_H_INDEX;
UINT8 gCWF_L_INDEX;
UINT8 gTUNG_H_INDEX;
UINT8 gTUNG_L_INDEX;
UINT8 gCLOUDY_H_INDEX;
UINT8 gCLOUDY_L_INDEX;

UINT16 ctc_rgain[4]={226,284,347,0};
UINT16 ctc_bgain[4]={378,360,343,0};

#define SP_AWBCTT_TBL_MAX 1

code UINT16 SP_AWBCTT_TBL[SP_AWBCTT_TBL_MAX][3] = {
{383,295,2}
};
UINT8 gSP_AWBCTT_TBL_MAX;

#define MCLKMODE        0 /*mclk mode,'1'from I/O Pad,'0'from internal*/
#define PCLKMODE        1 /*pclk mode,'1'from I/O Pad,'0'from internal*/
#define PREV_MCLKDIV    4/*MCLK div, master clock = (96MHz / mclkdiv)*/
#define PREV_PCLKDIV    2 /*pixel clock = (master clock) / pclkdiv*/
#define SNAP_MCLKDIV    4 /*MCLK div, master clock = (96MHz / mclkdiv)*/
#define SNAP_PCLKDIV    1 /*pixel clock = (master clock) / pclkdiv*/
#if (PCLKMODE == 1)
#define PREV_PCLKFREQ (UINT32) 12000000
#define SNAP_PCLKFREQ (UINT32) 12000000
#define CLK_RATIO     (UINT8) ((SNAP_PCLKFREQ/1000) * 128 / (PREV_PCLKFREQ/1000))
#else
#define PREV_PCLKFREQ (UINT32) 96000000 / PREV_MCLKDIV / PREV_PCLKDIV
#define SNAP_PCLKFREQ (UINT32) 96000000 / SNAP_MCLKDIV / SNAP_PCLKDIV
#define CLK_RATIO     (UINT8) ((UINT16) PREV_MCLKDIV * PREV_PCLKDIV * 128 / SNAP_MCLKDIV / SNAP_PCLKDIV)
#endif

#define PCLKphase   0
#define TGGPIOFUNC 0
#define TGGPIODIR 0x04
#define TGGPIOVAL 0

xdata UINT16 exptime;
extern xdata UINT8 PreGain;
extern xdata UINT32 PreShutter;
extern xdata UINT32 G_SHUTTER;
extern xdata UINT8 G_GAIN;

extern UINT8 HAL_CDSPeofWait( UINT8 Number);
extern void cdspColorMatrixRestore_Intr(void);
extern void cdspColorMatrixRestore(void);
extern UINT8 HAL_CDSPeofWait( UINT8 Number);


void  HAL_GCI2C_Write(UINT8 RegAddr, UINT8 RegData)
{
  UINT8 reg_addr[1], reg_data[1];
  reg_addr[0] = RegAddr;
  reg_data[0] = RegData;
  HAL_WriteSSC(reg_addr, reg_data, 0x01, 0x00);  //Set Seq SSC Write
}

void  HAL_FrontInit (UINT8 CamMode) USING_0
{
	UINT8 tmp0;

	printf (" Initialize the Front Sensor_GC1303 : Enter !!,CamMode=%d \n",CamMode);
	HAL_SetFrontIOConfig(TGGPIOFUNC, 0x00, TGGPIODIR, TGGPIOVAL);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	tmp0 = XBYTE[0x20E4];
	tmp0 &= 0xF0;
	tmp0 |= PCLKphase;
	HAL_SSCConfig(Sensor_Slave_Address, Sensor_SSC_Freq);
	XBYTE[0x2AB0] = 0x01;		      //Reset Sensor interface
	XBYTE[0x2AB0] = 0x00;
	HAL_SensorRegInit();
	gDAY_H_INDEX = DAY_H_INDEX;
	gDAY_L_INDEX = DAY_L_INDEX;
	gCWF_H_INDEX = CWF_H_INDEX;
	gCWF_L_INDEX = CWF_L_INDEX;
	gTUNG_H_INDEX = TUNG_H_INDEX;
	gTUNG_L_INDEX = TUNG_L_INDEX;
	gCLOUDY_H_INDEX = CLOUDY_H_INDEX;
	gCLOUDY_L_INDEX = CLOUDY_L_INDEX;
	gSP_AWBCTT_TBL_MAX = SP_AWBCTT_TBL_MAX;
	printf (" Initialize the Front : Exit !! \n");
}

void  HAL_FrontPreviewSet(void) USING_0
{
	HAL_FrontVdWait(0,1);
	HAL_SetFrontCLKSource(MCLKMODE, PCLKMODE);
	HAL_SetFrontCLKSet(PREV_MCLKDIV, PREV_PCLKDIV, 0);
	HAL_SetFrontImgCrop(Sensor_Preview_HOffset, Sensor_Preview_VOffset, Sensor_Preview_HSize, Sensor_Preview_VSize, 0x00);
	HAL_SetFrontInverse(Prev_Hsync_Inverse,Prev_Vsync_Inverse);
	HAL_SetFrontReshape(Sensor_Preview_Hfall, Sensor_Preview_Hrise, Sensor_Preview_HReshen, Sensor_Preview_Vfall, Sensor_Preview_Vrise, Sensor_Preview_VReshen);
	HAL_SetSensorPreview();
	XBYTE[0x21D3] = 0x04;
	XBYTE[0x21D5] = 0x80;
	XBYTE[0x21D8] = 0x73;
	XBYTE[0x2110]=Sensor_Preview_BayerPattern;
}
void  HAL_FrontSnapSet(UINT16 Hsize,UINT16 Vsize) USING_0
{
	HAL_SetFrontCLKSet(SNAP_MCLKDIV, SNAP_PCLKDIV, 0);
	HAL_SetFrontReshape(Sensor_Snap_Hfall, Sensor_Snap_Hrise, Sensor_Snap_HReshen, Sensor_Snap_Vfall, Sensor_Snap_Vrise, Sensor_Snap_VReshen);
	HAL_SetFrontImgCrop(Sensor_Snap_HOffset, Sensor_Snap_VOffset, Hsize, Vsize, 1);
	XBYTE[0x2110]=Sensor_Snap_BayerPattern;
	PreGain =0;
	PreShutter = 0;
//	XBYTE[0x21D5] = 0x18;
//	XBYTE[0x21D8] = 0x63;
}
void  HAL_SensorRegInit(void)
{
	HAL_Wait(20);
	HAL_GCI2C_Write(0x13, 0x80);
	HAL_Wait(100);
	HAL_GCI2C_Write(0x13, 0x00);
	HAL_GCI2C_Write(0x01, 0x14);
	HAL_GCI2C_Write(0x02, 0x10);
	HAL_GCI2C_Write(0x03, 0x01);
	HAL_GCI2C_Write(0x04, 0xf4);
	HAL_GCI2C_Write(0x05, 0x00);
	HAL_GCI2C_Write(0x06, 0x00);
	HAL_GCI2C_Write(0x07, 0x00);
	HAL_GCI2C_Write(0x08, 0x00);
	HAL_GCI2C_Write(0x09, 0x01);
	HAL_GCI2C_Write(0x0a, 0xe8);
	HAL_GCI2C_Write(0x0b, 0x02);
	HAL_GCI2C_Write(0x0c, 0x90);
	HAL_GCI2C_Write(0x0d, 0x01);  //0x03
	HAL_GCI2C_Write(0x0f, 0x00);	 // mirror
	HAL_GCI2C_Write(0x10, 0x24);
	HAL_GCI2C_Write(0x11, 0x00);
	HAL_GCI2C_Write(0x12, 0x10);
	HAL_GCI2C_Write(0x14, 0x00);
	HAL_GCI2C_Write(0x15, 0x08);
	HAL_GCI2C_Write(0x16, 0x06);
	HAL_GCI2C_Write(0x17, 0x08);
	HAL_GCI2C_Write(0x18, 0x02);
	HAL_GCI2C_Write(0x19, 0x00);//44
	HAL_GCI2C_Write(0x1a, 0x00);//44
	HAL_GCI2C_Write(0x1b, 0x00);
	HAL_GCI2C_Write(0x1c, 0x02);
	HAL_GCI2C_Write(0x1d, 0x02);
	HAL_GCI2C_Write(0x1e, 0x00);
	HAL_GCI2C_Write(0x1f, 0x00);
	HAL_GCI2C_Write(0x35, 0x6d);
	HAL_GCI2C_Write(0x36, 0x28);
	HAL_GCI2C_Write(0x40, 0x0c); //0x0d
	HAL_GCI2C_Write(0x41, 0x00);
	HAL_GCI2C_Write(0x42, 0x30);
	HAL_GCI2C_Write(0x43, 0x40);
	HAL_GCI2C_Write(0x44, 0xf8);
	HAL_GCI2C_Write(0x47, 0x46);
	HAL_GCI2C_Write(0x4e, 0x26);
	HAL_GCI2C_Write(0x59, 0x30);
	HAL_GCI2C_Write(0x61, 0x80);
	HAL_GCI2C_Write(0x63, 0x80);
	HAL_GCI2C_Write(0x65, 0x80);
	HAL_GCI2C_Write(0x67, 0x80);
	HAL_GCI2C_Write(0x68, 0x05);
	HAL_GCI2C_Write(0x70, 0x03);
	HAL_GCI2C_Write(0x71, 0x03);
	HAL_GCI2C_Write(0x72, 0x03);
	HAL_GCI2C_Write(0x73, 0x20);
	HAL_GCI2C_Write(0x74, 0x3d);
	HAL_GCI2C_Write(0x75, 0x51);
	HAL_GCI2C_Write(0x76, 0x00);
	HAL_GCI2C_Write(0x7d, 0x00);
	HAL_GCI2C_Write(0x7e, 0x44);
	HAL_GCI2C_Write(0x7f, 0x84);
	HAL_GCI2C_Write(0x80, 0x0c);//10
	HAL_GCI2C_Write(0x81, 0x0c);//10
	HAL_GCI2C_Write(0x82, 0x88); //44
	HAL_GCI2C_Write(0x83, 0x18); //14
	HAL_GCI2C_Write(0x84, 0x18);//40
	HAL_GCI2C_Write(0x85, 0x04);
	HAL_GCI2C_Write(0x86, 0xff);
	HAL_GCI2C_Write(0x87, 0x55);//93
	HAL_GCI2C_Write(0xd6, 0x98);
	HAL_GCI2C_Write(0xd8, 0x00);
	HAL_GCI2C_Write(0xdb, 0x40);
	HAL_GCI2C_Write(0xdc, 0xc0);
	HAL_GCI2C_Write(0x68, 0x05);
	printf("init---------------------------1\n");
}

void HAL_SetSensorPreview(void) USING_0
{
//	HAL_GCI2C_Write(0xdc, ttttt);

}
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontGainSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Sensor gain                                                           *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust gain value for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, gain value to set                                                    *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT8 HAL_FrontGainSet(
    UINT32 value,
    UINT32  option
)
{
#if 1
	UINT32 tmp0;
	UINT8 regaddr[2], regdata[2];
	UINT8 gainvalue;

	tmp0 = option & 0x01;                 /* Sync VD ? */
	gainvalue= gaintbl[value];
	regaddr[0] = 0xdc;
	regdata[0] =  gainvalue ;
	HAL_WriteSSC(regaddr, regdata, 0x01, tmp0 | 0x02);
#endif

	return SUCCESS;
}

UINT8 HAL_FrontGainSet_Intr(
    UINT32 value,
    UINT32  option
)USING_1
{

#if 1
	UINT32 tmp0;
	UINT8 regaddr[1], regdata[1];
	UINT8 gainvalue;

	tmp0 = option & 0x01;                 /* Sync VD ? */
	gainvalue= gaintbl[value];
	snapgain=gainvalue;
	regaddr[0] = 0xdc;
	regdata[0] = gainvalue ;
	HAL_WriteSSC(regaddr, regdata, 0x01, tmp0 | 0x02);
#endif
	return SUCCESS;
}
/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontExpTimeSet                                                  *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control exposure time                                                         *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust exposure time value                                                       *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, expossure time value to set, unit 0.1ms                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/
UINT32
HAL_FrontExpTimeSet(
	UINT32 value,
	UINT32 option
)
{
#if 1
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk, tmp0;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	regaddr[0] = 0x03;
	regdata[0] = ((espline>>8)&0x0f);
	regaddr[1] = 0x04;
	regdata[1] = (espline&0xff);
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
#endif

	return SUCCESS;
}


UINT32
HAL_FrontExpTimeSet_Intr(
	UINT32 value,
	UINT32 option
)USING_1
{
#if 1
	UINT16 ltotal, ftotal, espline;
	UINT32 pixclk, tmp0;
	UINT8  regaddr[2], regdata[2];

	ltotal = Sensor_Preview_LineTotal;	/* Read line total */
	ftotal = Sensor_Preview_FrameTotal; /* Read frame total */
	pixclk = PREV_PCLKFREQ;

	espline = (pixclk * 10) / (ltotal * value);
	option = option & 0x01;

	snapexp=espline;

	regaddr[0] = 0x03;
	regdata[0] = ((espline>>8)&0x0f);
	regaddr[1] = 0x04;
	regdata[1] = (espline&0xff);
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
#endif
	return SUCCESS;
}

/**************************************************************************************
*                                                                                     *
*  Function Name : HAL_FrontFrameRateSet                                                *
*                                                                                     *
*  Purposes      :                                                                    *
*    To control Frame Rate                                                            *
*                                                                                     *
*  Desprictions  :                                                                    *
*    Adjust Frame rate for sensor                                                     *
*                                                                                     *
*  Arguments     :                                                                    *
*    value : in, frame rate value to set                                              *
*    option : in, 0 -> immediate change value                                         *
*                 1 -> synchronize the value to the next Vsync                        *
*                                                                                     *
*  Returns       :                                                                    *
*    Error message                                                                    *
*                                                                                     *
*  See also      :                                                                    *
*                                                                                     *
***************************************************************************************/

UINT32
HAL_FrontFrameRateSet(
    UINT32 value,
    UINT32 option
)
{
#if 0
	UINT32 ltotal, ftotal;
	UINT32 pixclk, tmp0;
	//UINT32 espline;
	//UINT32 status;
	UINT8 regaddr[4], regdata[4];

    ltotal = Sensor_Preview_LineTotal;	/* Read line total */
    ftotal = Sensor_Preview_FrameTotal; /* Read frame total */

	pixclk = PREV_PCLKFREQ;
	option = option & 0x01;

	tmp0=(pixclk * 10) /(ltotal * value);

	if (tmp0 >= ftotal ) {
		tmp0 =tmp0-ftotal;
	}
	else {
	    tmp0 =0;
	};

	regaddr[0] = 0x46;//0x2D;
	regdata[0] = tmp0;
	regaddr[1] = 0x47;//0x2E;
	regdata[1] = tmp0 >> 8;
	HAL_WriteSSC(regaddr, regdata, 0x02, option | 0x02);
#endif
	return 0;
}

void HAL_ExpTransToSnap(void)
{
#if 1
	UINT16 espline;
	UINT8  regaddr[3], regdata[3];
	UINT8 s_gain;

	espline=snapexp;
	s_gain=snapgain;

	if (s_gain<96)
	{
		espline=espline;
		s_gain=s_gain;
	}
	else if (s_gain<192)
	{
		espline=espline*2;
		s_gain=s_gain/2;
	}
	else
	{
		espline=espline*3;
		s_gain=s_gain/3;
	}
	regaddr[0] = 0x03;
	regdata[0] = ((espline>>8)&0x0f);
	regaddr[1] = 0x04;
	regdata[1] = (espline&0xff) ;
	regaddr[2] = 0xdc;
	regdata[2] = s_gain ;
	HAL_WriteSSC(regaddr, regdata, 0x03,  0x03);

#endif

}

UINT8 HAL_SnapRaw8(UINT8 mode, UINT8 opt) USING_0
{
	mode = mode;

	if( opt == 1 ) {
		HAL_FrontVvalidWait(1,1);
		//HAL_GCI2C_Write(0x40, 0x09);
		//HAL_GCI2C_Write(0x40, 0x09);
		HAL_ExpTransToSnap();
	}

	HAL_FrontVvalidWait(0,1); // 20081024 mantis 32544 wenguo
	HAL_CdspWriteSkip( 6, 1 );
	HAL_FrontVvalidWait(0,1);
	HAL_FrontVvalidWait(1,1);
	HAL_FrontInputGate(0x11);
	HAL_CDSPeofWait(1);
	return 0;
}
void HalSensorPrePV(
	void
)
{
	HAL_FrontVvalidWait(0,1);
	HAL_FrontVvalidWait(0,1);
	return;
}
#if 1 /*cs added for gain*/

UINT8 HAL_MaxGainIndexGet()
{
	return(MAX_GAIN_IDX);
}

UINT8 HAL_MinGainIndexGet()
{
	return(MIN_GAIN_IDX);
}

UINT8 HAL_MaxExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MAX_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MAX_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MAX_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MAX_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

UINT8 HAL_MinExpIndexGet(
UINT16 frame
)
{
	switch (frame) {
		case SP1K_FRAME_RATE_30:
			return(MIN_FRAME_RATE_30_IDX);
			break;
		case SP1K_FRAME_RATE_15:
			return(MIN_FRAME_RATE_15_IDX);
			break;
		case SP1K_FRAME_RATE_25:
			return(MIN_FRAME_RATE_25_IDX);
			break;
		case SP1K_FRAME_RATE_12P5:
			return(MIN_FRAME_RATE_12P5_IDX);
			break;
		default:
			return FAIL;
	};
}

void HAL_ColorSatReduce_Intr(UINT8 idx)	   // reduce color saturation and edge
{
#if 0
	if(idx > K_GAIN_Large) {
		if( sensorDriverGainClass != 3 ) {
			HAL_CdspColorMSet_Intr(cmt[2][0],cmt[2][1],cmt[2][2],cmt[2][3],cmt[2][4],cmt[2][5],cmt[2][6],cmt[2][7],cmt[2][8]);
			XBYTE[0x21D3] = 0x08;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 3;
		}
	}
	else if (idx > K_GAIN_Middle) {
		if( sensorDriverGainClass != 2 ) {
			HAL_CdspColorMSet_Intr(cmt[1][0],cmt[1][1],cmt[1][2],cmt[1][3],cmt[1][4],cmt[1][5],cmt[1][6],cmt[1][7],cmt[1][8]);
			XBYTE[0x21D3] = 0x06;
			XBYTE[0x21D0] = 0x08;

			sensorDriverGainClass = 2;
		}
	}
	else if (idx > K_GAIN_Small) {
		if( sensorDriverGainClass != 1 ) {
			HAL_CdspColorMSet_Intr(cmt[0][0],cmt[0][1],cmt[0][2],cmt[0][3],cmt[0][4],cmt[0][5],cmt[0][6],cmt[0][7],cmt[0][8]); // for hgnoise
			XBYTE[0x21D3] = 0x04;
			XBYTE[0x21D0] = 0x0A;

			sensorDriverGainClass = 1;
		}
	}
	else {
		if( sensorDriverGainClass != 0 ) {
			cdspColorMatrixRestore_Intr();
			XBYTE[0x21D3] = 0x02;
			XBYTE[0x21D0] = 0x02;

			sensorDriverGainClass = 0;
		}
	};
#endif
}
#endif
#endif
